package com.jw.blog.wblog.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jw.blog.annotation.SystemLog;
import com.jw.blog.utils.ConstantUtil;
import com.jw.blog.wblog.entity.Resource;
import com.jw.blog.wblog.entity.custom.ResultBean;
import com.jw.blog.wblog.service.IResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 无良芳
 * @since 2020-06-17
 */
@RestController
public class ResourceController {

    @Autowired
    private IResourceService iResourceService;

    @GetMapping("/selectLikeResourceListByPage")
    @SystemLog(description = ConstantUtil.RESOURCE_FINDKEY,userType= ConstantUtil.USERTYPE_USER)
    public ResultBean<IPage<Resource>> selectLikeResourceListByPage(
            Resource resource,
            String prarm,
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize",  defaultValue = "9") Integer pageSize
    ){
        QueryWrapper<Resource> resourceQueryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(resource.getTitle())){
            resourceQueryWrapper.lambda().like(Resource::getTitle,resource.getTitle());
        }

        if (StrUtil.isNotBlank(resource.getContent())){
            resourceQueryWrapper.lambda().like(Resource::getContent,resource.getContent());
        }
        if (resource.getStatus() != null){
            resourceQueryWrapper.lambda().eq(Resource::getStatus,resource.getStatus());
        }
        resourceQueryWrapper.lambda().orderByDesc(Resource::getAddTime);
        Page<Resource> page1 = new Page<>();
        page1.setCurrent(page);
        page1.setSize(pageSize);
        Page<Resource> resourcePage = iResourceService.page(page1, resourceQueryWrapper);
        ResultBean<IPage<Resource>> resultBean = new ResultBean<>();
        if (resourcePage.getRecords().size() != 0){
            resultBean.setData(resourcePage);
        }else {
            resultBean.setCode(ResultBean.FAIL);
        }
        return resultBean;
    }
}
